package mju.cxf.config.scheduleds;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import mju.cxf.entity.Product;
import mju.cxf.entity.ProductCategories;
import mju.cxf.entity.ProductSpecifications;
import mju.cxf.entity.Sales;
import mju.cxf.service.ProductCategoriesService;
import mju.cxf.service.ProductService;
import mju.cxf.service.ProductSpecificationsService;
import mju.cxf.service.SalesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Component
public class SalesScheduled {
    @Autowired
    private SalesService salesService;
    @Autowired
    private ProductSpecificationsService pss;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductCategoriesService pcs;
    @Scheduled(cron = "30 59 23 * * *") //每天23时59分30秒
    public void salesCancel() {
        LambdaQueryWrapper<Sales> sqw = new LambdaQueryWrapper<>();
        sqw.eq(Sales::getState,2);
        List<Sales> cancelSales = new ArrayList<>();
        for (Sales sales : salesService.list(sqw)) {
            final Date keyOutTime = sales.getKeyOutTime();
            final Date currentTime = new Date();
            // 判断过期时间是否大于等于当前时间
            if (keyOutTime == null || keyOutTime.before(currentTime)) {
                // 卡密已过期，执行取消操作
                sales.setState(6);
                cancelSales.add(sales);
                continue;
            }
            final Date shelfTime = sales.getShelfTime();
            //卡密已售卖时间 下架操作
            if (shelfTime == null || shelfTime.before(currentTime)){
                sales.setState(3);
                cancelSales.add(sales);
            }
        }
        if (!cancelSales.isEmpty()) {
            salesService.updateBatchById(cancelSales);
        }

        log.info("成功处理了{}条出售信息", cancelSales.size());
    }
    @Scheduled(cron = "*/5 * * * * *") //每天23时59分30秒
    public void salestuijianCancel() {
        HashMap<Integer,Integer> hashMap=new HashMap<>();
      LambdaQueryWrapper<Product> sqw = new LambdaQueryWrapper<>();
      sqw.eq(Product::getState,0);
      productService.list(sqw).forEach(product -> {
          AtomicReference<Integer> cli= new AtomicReference<>(0);
          LambdaQueryWrapper<ProductSpecifications> lqw=new LambdaQueryWrapper<>();
         pss.list(lqw.eq(ProductSpecifications::getProductId,product.getId())).forEach(productSpecifications -> {
            cli.set(cli.get() + productSpecifications.getClicks());
         });
         hashMap.put(product.getId(),cli.get());
      });
        // 将 HashMap 转换成 List
        List<Map.Entry<Integer, Integer>> entryList = new ArrayList<>(hashMap.entrySet());

        // 根据值进行从大到小排序
        entryList.sort((entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));

        // 输出排序后的结果
        ArrayList<Integer> remen=new ArrayList<>();
        for (Map.Entry<Integer, Integer> entry : entryList) {
            remen.add(entry.getKey());
        }
        LambdaQueryWrapper<ProductCategories> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ProductCategories::getCategoriesId,1);
        pcs.remove(lqw);
        int j= Math.min(remen.size(), 5);
        for (int i = 0; i < j; i++) {

            ProductCategories productCategories = new ProductCategories();
            productCategories.setCategoriesId(1);
            productCategories.setProductId(remen.get(i));
            pcs.save(productCategories);
        }
    }

}
